import subprocess
from concurrent.futures import ThreadPoolExecutor, as_completed
import time

start_time = time.time()
# 定义执行 curl 请求的函数
def run_curl():
    curl_command = """
EVENT_ID=$(curl -X POST https://b73a.prod-bd-wx-if.apps.gitee-ai.com/call/infer?sign=eyJhbGciOiJFZERTQSJ9.eyJpc3MiOiJodHRwczovL2FpLmdpdGVlLmNvbSIsInN1YiI6IntcIm5hbWVzcGFjZVwiOlwic3RyaW5naWZ5XCIsXCJwcm9qZWN0XCI6XCJLb2xvcnNcIixcImFwcElkXCI6XCJiNzNhXCJ9IiwiZXhwIjoxNzI2MTI5NzcxfQ.dOrw9RnQyj5Cax35Zmq9IM0wzSO-vYPZtCvCGQYalBDbIix-wbokYmOJuSPE-caXyPCKezSzUZqKUZR-A1djAw \
-H "Content-Type: application/json" \
-d '{"data": ["一张瓢虫的照片，微距，变焦，高质量，电影，瓢虫拿着一个木牌，上面写着“我爱 Gitee” 的文字","残缺的文字, 残缺的手指，畸形的手指，畸形的手，残肢，低质量、色情",1,true,1024,1024,7,25]}' \
| awk -F'"' '{ print $4 }') && curl https://b73a.prod-bd-wx-if.apps.gitee-ai.com/call/infer/$EVENT_ID?sign=eyJhbGciOiJFZERTQSJ9.eyJpc3MiOiJodHRwczovL2FpLmdpdGVlLmNvbSIsInN1YiI6IntcIm5hbWVzcGFjZVwiOlwic3RyaW5naWZ5XCIsXCJwcm9qZWN0XCI6XCJLb2xvcnNcIixcImFwcElkXCI6XCJiNzNhXCJ9IiwiZXhwIjoxNzI2MTI5NzcxfQ.dOrw9RnQyj5Cax35Zmq9IM0wzSO-vYPZtCvCGQYalBDbIix-wbokYmOJuSPE-caXyPCKezSzUZqKUZR-A1djAw \
| awk -F'"url":' '{print $2}' | awk -F'"' '{print $2}'
"""
    result = subprocess.run(curl_command, shell=True, capture_output=True, text=True)
    return result.stdout

# 使用线程池执行并发任务
def run_concurrent_curls(num_requests=10):
    with ThreadPoolExecutor(max_workers=num_requests) as executor:
        # 提交所有 curl 请求
        futures = [executor.submit(run_curl) for _ in range(num_requests)]
        
        # 显式等待每个任务完成并获取结果
        for future in as_completed(futures):
            try:
                print(future.result())  # 输出每个任务的结果
                print("耗时:",time.time() - start_time)
            except Exception as e:
                print(f"Error occurred: {e}")


run_concurrent_curls(10)

#!curl -X POST -H "Content-Type: application/json" -d '{"prompt":"中国汉服美女, 衣服写着Gitee", "num_inference_steps": "30"}' https://r2wd.prod-bd-wx-if.apps.gitee-ai.com/generate?sign=eyJhbGciOiJFZERTQSJ9.eyJpc3MiOiJodHRwczovL2FpLmdpdGVlLmNvbSIsInN1YiI6IntcIm5hbWVzcGFjZVwiOlwic3RyaW5naWZ5XCIsXCJwcm9qZWN0XCI6XCJLb2xvcnMtQVBJXCIsXCJhcHBJZFwiOlwicjJ3ZFwifSIsImV4cCI6MTcyNjE0MjIyMH0.3CUvDqlfHLfkMpGRKzsQsI48PPdcWFdpmIevtalH27969WCyGoinqQouB-rM5ALyiZ50N81dfU-K7BKRVHPyCg -o cc.png